DROP TRIGGER UPDATE_PERSON_ID_TRI;
DROP SEQUENCE PERSON_ID_SEQ;
DROP TABLE PERSONS;

--创建表
CREATE TABLE PERSONS(
  PERSON_ID INT PRIMARY KEY,
  PERSON_NAME VARCHAR2(20) NOT NULL,
  SEX INTEGER NOT NULL,
  CONSTRAINT PERSONS_SEX_CHK CHECK(SEX IN(1,0)),
  BIRTHDATE DATE NOT NULL,
  ADDRESS VARCHAR2(100) NOT NULL
);

--创建序列
CREATE SEQUENCE PERSON_ID_SEQ
MAXVALUE 1024
INCREMENT BY 1
NOCACHE
NOORDER
CYCLE;

--创建触发器
CREATE TRIGGER UPDATE_PERSON_ID_TRI BEFORE
INSERT ON PERSONS FOR EACH ROW WHEN(NEW.PERSON_ID IS NULL)
BEGIN
SELECT PERSON_ID_SEQ.NEXTVAL INTO:NEW.PERSON_ID FROM DUAL;
END;
